import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _class, _class2;

import React from 'react';
import { Col, Form, Input, InputNumber, Modal, Row, Select, Spin, Table } from 'antd';
import { Bind } from 'lodash-decorators';
import uuid from 'uuid/v4';
import Lov from 'components/Lov';
import Switch from 'components/Switch';
import { Button as ButtonPermission } from 'components/Permission';
import intl from 'utils/intl';
import { FORM_FIELD_CLASSNAME } from 'utils/constants';
import { CODE_UPPER } from 'utils/regExp';
import { operatorRender } from 'utils/renderer';
import ItemForm from "./ItemForm";
var FormItem = Form.Item;
var formLayout = {
  labelCol: {
    span: 5
  },
  wrapperCol: {
    span: 15
  }
};
var EmailForm = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_React$PureComponent) {
  _inherits(EmailForm, _React$PureComponent);

  var _super = _createSuper(EmailForm);

  function EmailForm(props) {
    var _this;

    _classCallCheck(this, EmailForm);

    _this = _super.call(this, props);
    _this.state = {
      modalVisible: false,
      itemFormData: {}
    };
    return _this;
  }
  /**
   * @function showItemModal - 显示新增配置属性模态框
   * @param {object} record - 行数据
   */


  _createClass(EmailForm, [{
    key: "showItemModal",
    value: function showItemModal() {
      var record = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      this.setState({
        modalVisible: true,
        itemFormData: record
      });
    }
    /**
     * @function handleDeleteItem - 删除服务器属性配置
     * @param {object} record - 行数据
     */

  }, {
    key: "handleDeleteItem",
    value: function handleDeleteItem(record) {
      var deleteItem = this.props.deleteItem;
      deleteItem(record);
    }
    /**
     * @function hideItemModal - 隐藏新增配置属性模态框
     */

  }, {
    key: "hideItemModal",
    value: function hideItemModal() {
      this.setState({
        modalVisible: false
      });
    }
    /**
     * @function handleAddItem - 新增一条邮箱服务器配置项
     * @param {object} params - 新增参数
     * @param {string} params.propertyCode - 服务器配置项 - 属性名称
     * @param {string} params.propertyName - 服务器配置项 - 属性值
     */

  }, {
    key: "handleAddItem",
    value: function handleAddItem(itemValue) {
      var addItem = this.props.addItem;
      var itemFormData = this.state.itemFormData;
      addItem(itemValue, itemFormData);
      this.hideItemModal();
    }
  }, {
    key: "handleEmailOk",
    value: function handleEmailOk() {
      var _this$props = this.props,
          form = _this$props.form,
          onOk = _this$props.onOk;
      form.validateFields(function (err, fieldsValue) {
        if (!err) {
          onOk(fieldsValue);
        }
      });
    }
    /**
     * 设置SSL默认值
     * @memberof EmailForm
     */

  }, {
    key: "setDefaultSSL",
    value: function setDefaultSSL() {
      var _this$props2 = this.props,
          form = _this$props2.form,
          addItem = _this$props2.addItem;
      var itemFormData = this.state.itemFormData;
      var arr = [{
        propertyId: uuid(),
        isCreate: true,
        propertyCode: 'mail.smtp.socketFactory.class',
        propertyValue: 'javax.net.ssl.SSLSocketFactory'
      }, {
        propertyId: uuid(),
        isCreate: true,
        propertyCode: 'mail.smtp.socketFactory.port',
        propertyValue: '465'
      }, {
        propertyId: uuid(),
        isCreate: true,
        propertyCode: 'mail.smtp.ssl.enable',
        propertyValue: 'true'
      }];
      form.setFieldsValue({
        port: 465
      });
      addItem(arr, itemFormData, true);
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props3 = this.props,
          form = _this$props3.form,
          initData = _this$props3.initData,
          path = _this$props3.path,
          itemList = _this$props3.itemList,
          title = _this$props3.title,
          modalVisible = _this$props3.modalVisible,
          loading = _this$props3.loading,
          onCancel = _this$props3.onCancel,
          isCopy = _this$props3.isCopy,
          tenantRoleLevel = _this$props3.tenantRoleLevel,
          _this$props3$detailLo = _this$props3.detailLoading,
          detailLoading = _this$props3$detailLo === void 0 ? false : _this$props3$detailLo,
          _this$props3$filterSt = _this$props3.filterStrategyList,
          filterStrategyList = _this$props3$filterSt === void 0 ? [] : _this$props3$filterSt,
          _this$props3$enums = _this$props3.enums;
      _this$props3$enums = _this$props3$enums === void 0 ? {} : _this$props3$enums;
      var _this$props3$enums$CM = _this$props3$enums['CMSG.EMAIL_PROTOCOL'],
          protocolList = _this$props3$enums$CM === void 0 ? [] : _this$props3$enums$CM;
      var getFieldDecorator = form.getFieldDecorator;
      var tenantId = initData.tenantId,
          tenantName = initData.tenantName,
          serverCode = initData.serverCode,
          serverName = initData.serverName,
          username = initData.username,
          passwordEncrypted = initData.passwordEncrypted,
          host = initData.host,
          port = initData.port,
          sender = initData.sender,
          protocol = initData.protocol,
          tryTimes = initData.tryTimes,
          enabledFlag = initData.enabledFlag,
          filterStrategy = initData.filterStrategy;
      var columns = [{
        title: intl.get('cmsg.email.model.email.propertyCode').d('属性编码'),
        dataIndex: 'propertyCode',
        width: 150
      }, {
        title: intl.get('cmsg.email.model.email.propertyValue').d('属性值'),
        dataIndex: 'propertyValue'
      }, {
        title: intl.get('cpaas.common.button.action').d('操作'),
        width: 120,
        dataIndex: 'edit',
        render: function render(text, record) {
          var operators = [];
          operators.push({
            key: 'edit',
            ele: /*#__PURE__*/React.createElement(ButtonPermission, {
              type: "text",
              permissionList: [{
                code: "".concat(path, ".button.editServer"),
                type: 'button',
                meaning: '邮箱账户-编辑服务器配置'
              }],
              onClick: function onClick() {
                return _this2.showItemModal(record);
              }
            }, intl.get('cpaas.common.button.edit').d('编辑')),
            len: 2,
            title: intl.get('cpaas.common.button.edit').d('编辑')
          }, {
            key: 'delete',
            ele: /*#__PURE__*/React.createElement(ButtonPermission, {
              type: "text",
              permissionList: [{
                code: "".concat(path, ".button.deleteServer"),
                type: 'button',
                meaning: '邮箱账户-删除服务器配置'
              }],
              onClick: function onClick() {
                return _this2.handleDeleteItem(record);
              }
            }, intl.get('cpaas.common.button.delete').d('删除')),
            len: 2,
            title: intl.get('cpaas.common.button.delete').d('删除')
          });
          return operatorRender(operators);
        }
      }];
      return /*#__PURE__*/React.createElement(Modal, {
        destroyOnClose: true,
        title: isCopy ? intl.get('cpaas.common.button.copy').d('复制') : title,
        visible: modalVisible,
        width: "1000px",
        confirmLoading: loading,
        wrapClassName: "ant-modal-sidebar-right",
        transitionName: "move-right",
        onCancel: onCancel,
        onOk: this.handleEmailOk
      }, /*#__PURE__*/React.createElement(Spin, {
        spinning: detailLoading
      }, /*#__PURE__*/React.createElement(Row, {
        type: "flex"
      }, !tenantRoleLevel && /*#__PURE__*/React.createElement(Col, {
        span: 12
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('entity.tenant.tag').d('租户')
      }), getFieldDecorator('tenantId', {
        initialValue: tenantId === 0 ? 0 : tenantId,
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('entity.tenant.tag').d('租户')
          })
        }]
      })( /*#__PURE__*/React.createElement(Lov, {
        disabled: !!tenantName,
        textValue: tenantName,
        code: "CPFM.TENANT"
      })))), /*#__PURE__*/React.createElement(Col, {
        span: 12
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('cmsg.common.view.accountCode').d('账户代码')
      }), getFieldDecorator('serverCode', {
        initialValue: serverCode,
        rules: [{
          type: 'string',
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cmsg.common.view.accountCode').d('账户代码')
          })
        }, {
          pattern: CODE_UPPER,
          message: intl.get('cpaas.common.validation.codeUpper').d('全大写及数字，必须以字母、数字开头，可包含“-”、“_”、“.”、“/”')
        }]
      })( /*#__PURE__*/React.createElement(Input, {
        typeCase: "upper",
        trim: true,
        inputChinese: false,
        disabled: !!tenantName && !isCopy
      })))), /*#__PURE__*/React.createElement(Col, {
        span: 12
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('cmsg.common.view.accountName').d('账户名称')
      }), getFieldDecorator('serverName', {
        initialValue: serverName,
        rules: [{
          type: 'string',
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cmsg.common.view.accountName').d('账户名称')
          })
        }]
      })( /*#__PURE__*/React.createElement(Input, null)))), /*#__PURE__*/React.createElement(Col, {
        span: 12
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('cmsg.email.model.email.userName').d('用户名')
      }), getFieldDecorator('username', {
        initialValue: isCopy ? undefined : username,
        rules: [{
          type: 'string',
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cmsg.email.model.email.userName').d('用户名')
          })
        }]
      })( /*#__PURE__*/React.createElement(Input, {
        trim: true
      })))), /*#__PURE__*/React.createElement(Col, {
        span: 12
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('cmsg.email.model.email.password').d('密码')
      }), getFieldDecorator('passwordEncrypted', {
        initialValue: isCopy ? undefined : passwordEncrypted,
        rules: [{
          type: 'string',
          required: false,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cmsg.email.model.email.password').d('密码')
          })
        }, {
          max: 110,
          message: intl.get('cpaas.common.validation.max', {
            max: 110
          })
        }]
      })( /*#__PURE__*/React.createElement(Input, {
        type: "password",
        placeholder: tenantName ? intl.get('cpaas.common.validation.notChange').d('未更改') : ''
      })))), /*#__PURE__*/React.createElement(Col, {
        span: 12
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('cmsg.email.model.email.host').d('邮件服务器')
      }), getFieldDecorator('host', {
        initialValue: host,
        rules: [{
          type: 'string',
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cmsg.email.model.email.host').d('邮件服务器')
          })
        }]
      })( /*#__PURE__*/React.createElement(Input, null)))), /*#__PURE__*/React.createElement(Col, {
        span: 12
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('cmsg.email.model.email.port').d('端口')
      }), getFieldDecorator('port', {
        initialValue: port,
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cmsg.email.model.email.port').d('端口')
          })
        }]
      })( /*#__PURE__*/React.createElement(InputNumber, {
        precision: 0,
        min: 1,
        max: 65535
      })))), /*#__PURE__*/React.createElement(Col, {
        span: 12
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('cmsg.email.model.email.protocol').d('协议')
      }), getFieldDecorator('protocol', {
        initialValue: protocol,
        rules: [{
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cmsg.email.model.email.protocol').d('协议')
          })
        }]
      })( /*#__PURE__*/React.createElement(Select, {
        className: FORM_FIELD_CLASSNAME
      }, protocolList.map(function (item) {
        return /*#__PURE__*/React.createElement(Select.Option, {
          key: item.value,
          value: item.value
        }, item.meaning);
      }))))), /*#__PURE__*/React.createElement(Col, {
        span: 12
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('cmsg.email.model.email.tryTimes').d('重试次数')
      }), getFieldDecorator('tryTimes', {
        initialValue: tryTimes,
        rules: [{
          type: 'number',
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cmsg.email.model.email.tryTimes').d('重试次数')
          })
        }]
      })( /*#__PURE__*/React.createElement(InputNumber, {
        min: 0,
        max: 5
      })))), /*#__PURE__*/React.createElement(Col, {
        span: 12
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('cmsg.email.model.email.sender').d('发送人')
      }), getFieldDecorator('sender', {
        initialValue: isCopy ? undefined : sender,
        rules: [{
          type: 'string',
          required: true,
          message: intl.get('cpaas.common.validation.notNull', {
            name: intl.get('cmsg.email.model.email.sender').d('发送人')
          })
        }]
      })( /*#__PURE__*/React.createElement(Input, null)))), /*#__PURE__*/React.createElement(Col, {
        span: 12
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('cpaas.common.status.enable').d('启用')
      }), getFieldDecorator('enabledFlag', {
        initialValue: enabledFlag === undefined ? 1 : enabledFlag
      })( /*#__PURE__*/React.createElement(Switch, null)))), /*#__PURE__*/React.createElement(Col, {
        span: 12
      }, /*#__PURE__*/React.createElement(FormItem, Object.assign({}, formLayout, {
        label: intl.get('cmsg.email.model.email.filterStrategy').d('安全策略')
      }), getFieldDecorator('filterStrategy', {
        initialValue: filterStrategy
      })( /*#__PURE__*/React.createElement(Select, {
        allowClear: true,
        className: FORM_FIELD_CLASSNAME
      }, filterStrategyList.map(function (item) {
        return /*#__PURE__*/React.createElement(Select.Option, {
          key: item.value,
          value: item.value
        }, item.meaning);
      })))))), /*#__PURE__*/React.createElement(Row, {
        type: "flex",
        justify: "end",
        style: {
          marginTop: '8px'
        }
      }, /*#__PURE__*/React.createElement(Col, {
        span: 23
      }, /*#__PURE__*/React.createElement("div", {
        className: "table-operator"
      }, /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.createServer"),
          type: 'button',
          meaning: '邮箱账户-新建服务器配置项'
        }],
        onClick: this.showItemModal
      }, intl.get('cmsg.email.view.message.title.modal.server.create').d('新建服务器配置项')), /*#__PURE__*/React.createElement(ButtonPermission, {
        permissionList: [{
          code: "".concat(path, ".button.setSSLDefault"),
          type: 'button',
          meaning: '邮箱账户-设置SSL属性'
        }],
        onClick: this.setDefaultSSL
      }, intl.get('cmsg.email.view.button.setDefaultSSL').d('设置SSL属性'))), /*#__PURE__*/React.createElement(Table, {
        bordered: true,
        rowKey: "propertyId",
        dataSource: itemList,
        columns: columns,
        pagination: false
      }))), /*#__PURE__*/React.createElement(ItemForm, {
        title: "".concat(this.state.itemFormData.propertyId ? intl.get('cmsg.email.view.message.title.modal.server.edit').d('编辑服务器配置项') : intl.get('cmsg.email.view.message.title.modal.server.create').d('新建服务器配置项')),
        modalVisible: this.state.modalVisible,
        initData: this.state.itemFormData,
        onCancel: this.hideItemModal,
        onOk: this.handleAddItem
      })));
    }
  }]);

  return EmailForm;
}(React.PureComponent), (_applyDecoratedDescriptor(_class2.prototype, "showItemModal", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "showItemModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDeleteItem", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDeleteItem"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "hideItemModal", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "hideItemModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleAddItem", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleAddItem"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleEmailOk", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleEmailOk"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "setDefaultSSL", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "setDefaultSSL"), _class2.prototype)), _class2)) || _class);
export { EmailForm as default };